using log4net;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Weight.Base
{
    /// <summary>
    /// 服务管理类
    /// </summary>
    public  class ServiceManager
    {
        /// <summary>
        /// 环境运行路径
        /// </summary>
        public static string EnvironmentPath = string.Empty;

        /// <summary>
        ///用户配置路径
        /// </summary>
        public static string UserPath = string.Empty;

        /// <summary>
        /// 系统日志助手
        /// </summary>
        public static ILog Log ;

        /// <summary>
        /// 系统参数
        /// </summary>
        public static Setting SystemConfig;

        /// <summary>
        /// 配置文件助手
        /// </summary>
        public static XMLConfigManager<Setting> SystemManager;

        /// <summary>
        /// 词组配置
        /// </summary>
        public static PhraseConfigEntity PhraseConfig;
        /// <summary>
        /// 词组配置文件助手
        /// </summary>
        public static XMLConfigManager<PhraseConfigEntity> PhraseManager;

        /// <summary>
        /// 硬件配置
        /// </summary>
        public static XMLConfigManager<DeviceSetting> DeviceManager;

        /// <summary>
        /// 硬件设配配置
        /// </summary>
        public static DeviceSetting DevConfig;

        /// <summary>
        /// 格式化时间配置
        /// </summary>

        public class DateFormat
        {
            public static string mDateTime = "yyyy-MM-dd HH:mm:ss";
            public static string mDate = "yyyy-MM-dd";
            public static string mTime = "HH:mm:ss";
        }

   
        /// <summary>
        /// 互斥锁
        /// </summary>
        private static object mLocker = new object();

        private const int SettingVersion = 23;
        private const int DeviceVersion = 8;
        private const int PhraseVersion = 17;
        /// <summary>
        /// 初始化配置
        /// </summary>
        public static void initConfig()
        {
            lock(mLocker)
            {
                EnvironmentPath = Environment.CurrentDirectory;
                LoadSettingConfig();
                LoadDevConfig();
                LoadPhraseConfig();
                LogHelper.WriteLog("读取基础配置成功!");
            }
        }

        /// <summary>
        /// 加载设置配置
        /// </summary>
        public static void LoadSettingConfig()
        {
            SystemManager = new XMLConfigManager<Setting>("setting.xml", SettingVersion);
            SystemConfig = SystemManager.readConfig();
        }

        /// <summary>
        /// 加载设备配置
        /// </summary>
        public static void  LoadDevConfig()
        {
            DeviceManager = new XMLConfigManager<DeviceSetting>("Device.xml", DeviceVersion);
            DevConfig = DeviceManager.readConfig();
        }

        /// <summary>
        /// 加载词组配置
        /// </summary>
        public static void LoadPhraseConfig()
        {
            PhraseManager = new XMLConfigManager<PhraseConfigEntity>("Phrase.xml", PhraseVersion);
            PhraseConfig = PhraseManager.readConfig();
        }

       

        /// <summary>
        /// 添加环境变量
        /// </summary>
        /// <param name="paths"></param>
        public  static void AddEnvironmentPaths()
        {
            IEnumerable<string> paths = GetPaths();
            var path = new[] { Environment.GetEnvironmentVariable("PATH") ?? string.Empty };
            string newPath = string.Join(Path.PathSeparator.ToString(), path.Concat(paths));
            Environment.SetEnvironmentVariable("PATH", newPath);
        }

        /// <summary>
        ///获取设备路径
        /// </summary>
        /// <returns></returns>
        private static IEnumerable<string> GetPaths()
        {
            string rootPath = Environment.CurrentDirectory + "/DeviceDLL";
            string[] paths = Directory.GetDirectories(rootPath);
            List<string> list = new List<string>();
            list.Add(rootPath);
            foreach (string item in paths)
            {
                list.Add(item);
                string[] subPaths = Directory.GetDirectories(item);
                if (subPaths.Length > 0)
                {
                    list.AddRange(subPaths);
                }
            }
            return list.ToArray();
        }

    }
}


